/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycity.jsonops;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import com.mycity.dao.*;
import com.mycity.util.StringEncode;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;

/**
 *
 * @author celikeins
 */
public class UserOps {

    public static UserTab isCredentialsValid(String username, String password) {
        String url = "services/user/login?email=" + StringEncode.encode(username) + "&password=" + StringEncode.encode(password);
        UserTab user = null;
        try {
            JSONObject result = (JSONObject) JSONParse.readJSONFromUrl(url);
            if (result.get("email") != null) {
                JSONArray ach = (JSONArray) result.get("achievements");
                int as = ach.size();
                String[] achS = new String[as];
                for (int i = 0; i < as; i++) {
                    achS[i] = (String) ach.get(i);
                }

                user = new UserTab((Long) result.get("userId"), (String) result.get("email"),
                        (String) result.get("username"), (String) result.get("firstName"),
                        (String) result.get("lastName"), (String) result.get("password"),
                        (String) result.get("bio"), (String) result.get("profilePic"), achS, (Long) result.get("level"));
                return user;
            }
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return user;
    }

    public static UserTab createUser(UserTab user) {

        String url = "services/user/register?username=" + StringEncode.encode(user.getUsername())
                + "&name=" + StringEncode.encode(user.getFirstname()) + "&surname=" + StringEncode.encode(user.getLastname())
                + "&email=" + StringEncode.encode(user.getEmail()) + "&password=" + StringEncode.encode(user.getPassword());
        UserTab retUser = null;
        try {
            JSONObject result = (JSONObject) JSONParse.readJSONFromUrl(url);
            JSONArray ach = (JSONArray) result.get("achievements");
            int as = ach.size();
            String[] achS = new String[as];
            for (int i = 0; i < as; i++) {
                achS[i] = (String) ach.get(i);
            }
            if (result != null) {
                retUser = new UserTab((Long) result.get("userId"), (String) result.get("email"),
                        (String) result.get("username"), (String) result.get("firstName"),
                        (String) result.get("lastName"), (String) result.get("password"),
                        (String) result.get("bio"), (String) result.get("profilePic"), achS, (Long) result.get("level"));
                return retUser;
            }
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return retUser;

    }

    public static UserTab getUserById(Long id) {
        String url = "services/user/getUserById?userId=" + id;

        UserTab user = null;
        try {
            JSONObject result = (JSONObject) JSONParse.readJSONFromUrl(url);
            JSONArray ach = (JSONArray) result.get("achievements");
            int as = ach.size();
            String[] achS = new String[as];
            for (int i = 0; i < as; i++) {
                achS[i] = (String) ach.get(i);
            }
            if (result != null) {
                user = new UserTab((Long) result.get("userId"), (String) result.get("email"),
                        (String) result.get("username"), (String) result.get("firstName"),
                        (String) result.get("lastName"), (String) result.get("password"),
                        (String) result.get("bio"), (String) result.get("profilePic"), achS, (Long) result.get("level"));
                return user;
            }
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;
    }

    public static Boolean checkEmail(String email) {
        String url = "services/user/emailAvailable?email=" + StringEncode.encode(email);
        try {
            return JSONParse.readBoolFromUrl(url);
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;
    }

    public static Boolean checkUsername(String username) {
        String url = "services/user/usernameAvailable?username=" + StringEncode.encode(username);
        try {
            return JSONParse.readBoolFromUrl(url);
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;
    }

    public static Boolean giveDiamond(Long from, Long postId) {
        String url = "services/user/giveDiamond?postId=" + postId + "&userId=" + from;

        try {
            return JSONParse.readBoolFromUrl(url);
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;
    }

    public static Long giveStar(Long commentId) {
        String url = "services/user/giveStar?commentId=" + commentId;
        try {
            return JSONParse.readLongFromUrl(url);
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;
    }

    public static Boolean updateBio(Long userId, String bio) {
        String url = "services/user/updateBio?userId=" + userId + "&bio=" + StringEncode.encode(bio);
        try {
            return JSONParse.readLongFromUrl(url) > 0;
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;
    }

    public static Boolean changeProfilePic(Long userId, String id) {
        String url = "services/user/changeProfilePic?userId=" + userId + "&profilePic=" + id;
        try {
            return JSONParse.readLongFromUrl(url) > 0;
        } catch (IOException ex) {
            Logger.getLogger(UserOps.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ParseException ex) {
            return null;
        }
        return null;
    }
}
